//
//  HttpRequest.m
//  IWaneHome
//
//  Created by hwd on 印度历1938/4/7.
//  Copyright © 印度历1938年 hwd_zlg. All rights reserved.
//

#import "HttpRequest.h"
#import "AFNetworking.h"
@interface HttpRequest ()
@end
@implementation HttpRequest

//get通用接口Dic
+(void)GetrequestUrl:(NSString *)url Dic:(NSDictionary *)dic completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}

//post通用接口
+(void)PostrequestUrl:(NSString *)url Dic:(NSDictionary *)dic completion:(void (^)(NSDictionary *responseDic))complete
{
//    NSString *urlStringUTF8 = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    [manager POST:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     // 请求成功
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {//完成
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {//完成
            complete(dic);
        }
    }];
}
//post通用接口2 
+(void)PostMuTableDicrequestUrl:(NSString *)url Dic:(NSMutableDictionary *)dic Controller:(UIViewController *)selfVC completion:(void (^)(NSDictionary *responseDic))complete
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
        } progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 请求成功，解析数据
            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
            if (complete) {//完成
                dispatch_async(dispatch_get_main_queue(), ^{
                    complete(jsonDic);
                });
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            // 请求失败
            NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
            if (complete) {//完成
                dispatch_async(dispatch_get_main_queue(), ^{
                    complete(dic);
                });
            }
        }];
    });
}

+ (NSDictionary *)dictionaryWithJsonString:(NSData *)data {
    if (data == nil) {//判空
        return nil;
    }
    NSString *responseString = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {//错误
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
    
}

+(void)PostFileRequestUrl:(NSData *)data Dic:(NSDictionary *)dic  completion:(void (^)(NSDictionary *responseDic))complete{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager.requestSerializer setValue:@"JDZBNH3KZTYBT8ZZUO84R7INN7HRHIRJ" forHTTPHeaderField:@"X-Api-Key"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:[NSString stringWithFormat:@"%@",baseUrl] parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
        
        //上传的参数(上传图片，以文件流的格式)
        [formData appendPartWithFileData:data
         
                                    name:@"image"
         
                                fileName:@"imageName.jpg"
         
                                mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {//完成
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"errcode", nil];
        if (complete) {//完成
            complete(dic);
        }
    }];
}
@end
